<template>
  <div class="app-container">
    <!-- 页面标题区域 -->
    <div class="page-header">
      <h2>借款用户列表</h2>
      <p class="header-desc">管理所有借款用户信息，支持关键字检索和分页浏览</p>
    </div>

    <!-- 查询表单 -->
    <el-card class="search-card">
      <el-form :inline="true" class="search-form">
        <el-form-item label="关键字">
          <el-input
            v-model="keyword"
            placeholder="姓名/手机/身份证"
            clearable
            @keyup.enter.native="fetchData"
            class="search-input"
          />
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="fetchData"
          class="search-btn"
        >
          查询
        </el-button>
        <el-button
          type="default"
          @click="resetData"
          class="reset-btn"
        >
          清空
        </el-button>
      </el-form>
    </el-card>

    <!-- 列表区域 -->
    <el-card class="table-card">
      <el-table
        :data="list"
        stripe
        border
        v-loading="!list"
        element-loading-text="加载用户数据中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        :header-cell-style="headerCellStyle"
        :row-class-name="tableRowClassName"
      >
        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (page - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" min-width="100"/>
        <el-table-column prop="mobile" label="手机" min-width="120"/>
        <el-table-column prop="idCard" label="身份证号" min-width="200">
          <template slot-scope="scope">
            <span class="id-card-mask">{{ maskIdCard(scope.row.idCard) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="性别" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              size="mini"
              :type="scope.row.sex === 1 ? 'info' : 'success'"
            >
              {{ scope.row.sex === 1 ? '男' : '女' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="80" align="center"/>
        <el-table-column label="是否结婚" width="120" align="center">
          <template slot-scope="scope">
            <i
              class="el-icon-check"
              v-if="scope.row.isMarry"
              style="color: #4cd964; font-size: 18px;"
            ></i>
            <i
              class="el-icon-close"
              v-else
              style="color: #ff3b30; font-size: 18px;"
            ></i>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="120" align="center">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.status === 0"
              type="info"
              size="mini"
            >
              未认证
            </el-tag>
            <el-tag
              v-if="scope.row.status === 1"
              type="warning"
              size="mini"
            >
              认证中
            </el-tag>
            <el-tag
              v-if="scope.row.status === 2"
              type="success"
              size="mini"
            >
              认证通过
            </el-tag>
            <el-tag
              v-if="scope.row.status === -1"
              type="danger"
              size="mini"
            >
              认证失败
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" min-width="160"/>
        <el-table-column label="操作" min-width="200" align="center">
          <template slot-scope="scope">
            <router-link :to="'/core/borrower/detail/' + scope.row.id">
              <el-button
                v-if="scope.row.status === 1"
                type="warning"
                size="mini"
                class="operation-btn"
              >
                审批
              </el-button>
              <el-button
                v-else
                type="primary"
                size="mini"
                class="operation-btn"
              >
                查看
              </el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="page"
        :total="total"
        :page-size="limit"
        :page-sizes="[2, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="changePageSize"
        @current-change="changeCurrentPage"
        class="pagination"
      />
    </div>
  </div>
</template>

<script>
import borrowerApi from '@/api/core/borrower'

export default {
  data() {
    return {
      list: null, // 用户列表
      total: 0, // 总记录数
      page: 1, // 当前页码
      limit: 10, // 每页记录数
      keyword: '' // 搜索关键字
    }
  },
  created() {
    // 组件创建后加载数据
    this.fetchData()
  },
  methods: {
    // 加载用户列表数据
    fetchData() {
      // 显示加载状态
      const loading = this.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      borrowerApi
        .getPageList(this.page, this.limit, this.keyword)
        .then(response => {
          this.list = response.data.pageModel.records
          this.total = response.data.pageModel.total
          // 关闭加载状态
          loading.close()
        })
        .catch(error => {
          // 错误处理
          this.$message.error('数据加载失败，请重试')
          loading.close()
        })
    },
    // 改变每页显示数量
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 改变当前页码
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置搜索条件
    resetData() {
      this.keyword = ''
      this.fetchData()
    },
    // 身份证号掩码处理
    maskIdCard(idCard) {
      if (!idCard) return ''
      return idCard.replace(/(\d{6})(\d{8})(\d{4})/, '$1********$3')
    },
    // 表头样式
    headerCellStyle() {
      return {
        backgroundColor: '#f7f8fa',
        color: '#4e5969',
        fontWeight: '500',
        fontSize: '14px'
      }
    },
    // 表格行样式
    tableRowClassName({ row, rowIndex }) {
      return rowIndex % 2 === 0 ? 'even-row' : 'odd-row'
    }
  }
}
</script>

<style scoped lang="scss">
// 全局样式
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

// 页面标题
.page-header {
  margin-bottom: 20px;

  h2 {
    color: #1f2d3d;
    font-size: 20px;
    margin-bottom: 8px;
    font-weight: 500;
  }

  .header-desc {
    color: #8392a5;
    font-size: 14px;
  }
}

// 查询卡片
.search-card {
  margin-bottom: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 4px;
  overflow: hidden;
}

// 查询表单
.search-form {
  padding: 15px 0;

  .search-input {
    width: 300px;
    transition: all 0.3s;

    &:focus {
      box-shadow: 0 0 0 2px rgba(145, 163, 255, 0.2);
    }
  }

  .search-btn, .reset-btn {
    margin-left: 10px;
    transition: all 0.2s;

    &:hover {
      transform: translateY(-2px);
    }
  }
}

// 表格卡片
.table-card {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 20px;
}

// 表格样式
::v-deep .el-table {
  border-radius: 4px 4px 0 0;
  overflow: hidden;

  &::before {
    height: 0;
  }

  .el-table__header-wrapper {
    background-color: #f7f8fa;
  }

  .el-table__body tr:hover > td {
    background-color: #f0f7ff !important;
  }
}

// 表格行样式
.even-row {
  background-color: #fcfdfe;
}

// 身份证号掩码样式
.id-card-mask {
  letter-spacing: 1px;
}

// 操作按钮
.operation-btn {
  margin-right: 8px;
  transition: all 0.2s;

  &:hover {
    transform: translateY(-2px);
  }
}

// 分页容器
.pagination-container {
  display: flex;
  justify-content: flex-end;
  padding: 10px 0;
}

// 分页组件
.pagination {
  .el-pagination__sizes {
    margin-right: 10px;
  }

  .el-pagination__total {
    color: #8392a5;
  }

  .el-pager li {
    margin: 0 5px;
    border-radius: 4px;

    &:hover {
      color: #409eff;
    }

    &.active {
      background-color: #409eff;
      color: #fff;
      border-color: #409eff;
    }
  }
}

// 响应式调整
@media (max-width: 768px) {
  .app-container {
    padding: 10px;
  }

  .search-input {
    width: 180px !important;
  }
}
</style>
